<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改加班申请')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-overtime-edit" th:object="${hrOvertime}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label">申请人：</label>
                <div class="col-sm-8">
                    <input name="applyUserName" th:field="*{applyUserName}" class="form-control" type="text" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">申请时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="applyTime" th:value="${#dates.format(hrOvertime.applyTime, 'yyyy-MM-dd')}" class="form-control" readonly type="text">
                    </div>
                </div>
            </div>

            <div class="form-group">    
                <label class="col-sm-3 control-label">加班类型：</label>
                <div class="col-sm-8">
                    <select name="type" class="form-control m-b" th:with="type=${@dict.getType('overtime_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{type}"></option>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">原因：</label>
                <div class="col-sm-8">
                    <textarea name="reason" class="form-control">[[*{reason}]]</textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label"><span style="color: red; ">*</span>开始时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="startTime" th:value="${#dates.format(hrOvertime.startTime, 'yyyy-MM-dd HH:mm:ss')}"  readonly="readonly"  class="form-control calcTotalSecondInput" placeholder="yyyy-MM-dd" type="text">
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label"><span style="color: red; ">*</span>结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="endTime" th:value="${#dates.format(hrOvertime.endTime, 'yyyy-MM-dd HH:mm:ss')}"  readonly="readonly"  class="form-control calcTotalSecondInput" placeholder="yyyy-MM-dd" type="text">
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">加班时长：</label>
                <div class="col-sm-8">
                    <input name="totalTimes" th:field="*{totalTimes}" class="form-control" readonly="readonly" type="hidden" required>
                    <input name="totalTimeText" th:value="*{totalTimes + ' 小时'}" class="form-control" type="text" readonly>
                </div>
            </div>

        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script type="text/javascript">
        var prefix = ctx + "hr/overtime";
        $("#form-overtime-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form() && validateStartTimeAndEndTime()) {
                $.operate.save(prefix + "/edit", $('#form-overtime-edit').serialize());
            }
        }

        $("input[name='startTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii",
            minView: "hour",
            autoclose: true
        });

        $("input[name='endTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii",
            minView: "hour",
            autoclose: true
        });

        // 监听开始和结束日期填写，动态生成请假时长
        $(".calcTotalSecondInput").change(function () {

            $.ajax({
                type: 'POST',
                url: prefix + "/getOverTime",
                data: $('#form-overtime-edit').serialize(),
                dataType: 'json',
                success: function (result) {
                    if (result.msg == "success") {
                        console.log(result.data["hours"])
                        $('input[name="totalTimeText"]').val(result.data["hours"] + " 小时");
                        $('input[name="totalTimes"]').val(result.data["hours"]);
                    } else {
                        console.log(result.data)
                        $('input[name="totalTimeText"]').val(0 + " 小时");
                    }
                },
                error: function (error) {
                    $.modal.alertWarning("系统错误，请联系管理员");
                }
            });
        });

        function validateStartTimeAndEndTime() {
            if ($('input[name="startTime"]').val() > $('input[name="endTime"]').val()) {
                $.modal.alertWarning("结束时间必须大于开始时间");
                return false;
            }
            return true;
        }
    </script>
</body>
</html>